<?php
/**
 * Service_Mail
 *
 * Класс User_Notification осуществляет работу по оповещению пользователей о различных событиях
 *
 * @author Егоров Александр <slamer@slamer.ru>
 * @author Дмитрий Кузьма <kouzma.d@gmail.com>
 * @version 1.0
 */
class User_Notification {
  public function __construct() {}

  private static function _mailHello() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "hello"');
    return "<html>" . $text;
  }

  private static function _mailEnd() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "end"');
  	$text = str_replace('[site]', MAIL_SITE_NAME, $text);
  	$text = str_replace('[url]', W_FULLPATH, $text);
    return $text . "</html>";
  }

  /**
   * Метод уведомляет пользователя о создании нового аккаунта
   *
   * @param string $strEMail
   * @param string $login
   * @param string $pass
   */
  public static function newAccount($strEMail, $login, $pass, $code, $fio) {
    $m = new Service_Mail(self::_newAccountSubject(),
                    self::_mailHello() .
                    self::_newAccount($login, $code, $strEMail, $fio) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _newAccountSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "new_account"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _newAccount($login, $code, $strEMail, $fio) {
    $confirmationLink = W_FULLPATH . 'auth/approve/' . $code . '/' . $strEMail;
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "new_account"');
  	$text = str_replace('[site]', MAIL_FULL_SITE_NAME, $text);
  	$text = str_replace('[login]', $login, $text);
  	$text = str_replace('[name]', $fio, $text);
  	$text = str_replace('[url]', $confirmationLink, $text);
    return $text;
  }
  /**
   * Метод уведомляет туриста о создании нового аккаунта
   *
   * @param string $strEMail
   * @param string $login
   * @param string $pass
   */
  public static function newTouristAccount($strEMail, $login, $pass, $code, $fio) {
    $m = new Service_Mail(self::_newTouristAccountSubject(),
                    self::_mailHello() .
                    self::_newTouristAccount($login, $code, $strEMail, $fio) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _newTouristAccountSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "new_tourist_account"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _newTouristAccount($login, $code, $strEMail, $fio) {
    $confirmationLink = W_FULLPATH . 'auth/approve/' . $code . '/' . $strEMail;
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "new_tourist_account"');
  	$text = str_replace('[site]', MAIL_FULL_SITE_NAME, $text);
  	$text = str_replace('[login]', $login, $text);
  	$text = str_replace('[name]', $fio, $text);
  	$text = str_replace('[url]', $confirmationLink, $text);
    return $text;
  }

  /**
   * Метод уведомляет туриста о создании нового аккаунта
   *
   * @param string $strEMail
   * @param string $login
   * @param string $fio
   */
  public static function accountAccepted($strEMail, $login, $fio) {
    $m = new Service_Mail(self::_accountAcceptedSubject(),
                    self::_mailHello() .
                    self::_accountAccepted($login, $fio) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _accountAcceptedSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "account_accepted"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _accountAccepted($login, $fio) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "account_accepted"');
  	$text = str_replace('[name]', $fio, $text);
  	$text = str_replace('[login]', $login, $text);
    return $text;
  }


  /**
   * оповещение о новом отзыве
   * @param integer $strEMail - e-mail пользователя
   */
  public static function sendNotificationReview($strEMail) {
    $m = new Service_Mail(
    											self::_sendNotificationReviewSubject(),
			                    self::_mailHello() .
    											self::_sendNotificationReview() .
			                    self::_mailEnd(),
    											$strEMail);
	  $m->send();
  }

  private static function _sendNotificationReviewSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_notification_review"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _sendNotificationReview() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_notification_review"');
    return $text;
  }

  /**
   * Письмо от администратора
   * @param integer $strEMail - e-mail пользователя
   */
  public static function sendNotificationAdmin($strEMail, $text) {
    $m = new Service_Mail(
    											self::_sendNotificationAdminSubject(),
			                    self::_mailHello() .
    											$text .
			                    self::_mailEnd(),
    											$strEMail);
	  $m->send();
  }

  private static function _sendNotificationAdminSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_notification_admin"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  /**
   * оповещение о сообщении
   */
  public static function sendNotificationMessage($idUser, $idUserSender, $idChain) {
    $objUser = new User_User($idUser);
    $objSender = new User_User($idUserSender);
    $m = new Service_Mail(self::_sendNotificationMessageSubject($objSender),
    											self::_mailHello() .
    											self::_sendNotificationMessage($objSender, $idChain) .
    											self::_mailEnd(),
    											$objUser->getField('e_mail')
    );
    $m->send();
  }

  private static function _sendNotificationMessageSubject($objSender) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_notification_message"');
  	$text = str_replace('[name]', View_User::getUserName($objSender->tableFields), $text);
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

	private static function _sendNotificationMessage($objSender, $idChain) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_notification_message"');
  	$text = str_replace('[name]', View_User::getUserName($objSender->tableFields), $text);
  	$text = str_replace('[url]', W_FULLPATH . 'messages/chain/' . $idChain, $text);
    return $text;
	}

  /**
   * Метод уведомляет пользователя о появлении новой публикации от редактора
   *
   * @param string $strEMail
   * @param string $login
   * @param string $pass
   */
  public static function newPostNotification($strEMail, $postName, $postUrl) {
  	$m = new Service_Mail(self::_newPostNotificationSubject(),
                    self::_mailHello() .
                    self::_newPostNotification($postName, $postUrl) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _newPostNotificationSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "new_post_notification"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _newPostNotification($postName, $postUrl) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "new_post_notification"');
  	$text = str_replace('[site]', MAIL_FULL_SITE_NAME, $text);
  	$text = str_replace('[link]', "<a href='" . $postUrl . "' target='_blank' >" . $postName . "</a>", $text);
  	$text = str_replace('[link_url]', "<a href='" . $postUrl . "' target='_blank' >" . $postUrl . "</a>", $text);
    return $text;
  }

  /**
   * Метод уведомляет пользователя о появлении нового комментария
   *
   * @param string $strEMail
   * @param string $postName
   * @param string $postUrl
   */
  public static function newCommentNotification($strEMail, $postName, $postUrl, $isAuthor = false) {
  	$m = new Service_Mail(self::_newCommentNotificationSubject($isAuthor),
                    self::_mailHello() .
                    self::_newCommentNotification($postName, $postUrl, $isAuthor) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }


  private static function _newCommentNotificationSubject($isAuthor) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "new_comment_notification"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }


  private static function _newCommentNotification($postName, $postUrl, $isAuthor) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "new_comment_notification"');
  	$text = str_replace('[site]', MAIL_FULL_SITE_NAME, $text);
  	$text = str_replace('[your]', ($isAuthor ? "Your " : null), $text);
  	$text = str_replace('[link]', "<a href='" . $postUrl . "' target='_blank' >" . $postName . "</a>", $text);
  	$text = str_replace('[link_url]', "<a href='" . $postUrl . "' target='_blank' >" . $postUrl . "</a>", $text);
    return $text;
  }

  /**
   * Метод уведомляет пользователя о смене пароля
   *
   * @param string $strEMail
   */
  public static function changePass($strEMail, $pass) {
    $m = new Service_Mail(self::_changePassSubject(),
                    self::_mailHello() .
                    self::_changePass($pass) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _changePassSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "change_pass"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _changePass($pass) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "change_pass"');
  	$text = str_replace('[pass]', $pass, $text);
    return $text;
  }

  /**
   * Метод высылает восстановленный пароль
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function recoveryPass($strEMail, $pass, $userLogin = null) {
    $m = new Service_Mail(self::_recoveryPassSubject(),
                    self::_mailHello() .
                    self::_recoveryPass($pass, $userLogin) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _recoveryPassSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "recovery_pass"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _recoveryPass($pass, $userLogin) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "recovery_pass"');
  	$text = str_replace('[pass]', $pass, $text);
  	$text = str_replace('[login]', ($userLogin ? ('<b>Your login:</b> ' . $userLogin) : null), $text);
    return $text;
  }

 /**
   * Приветственно сообщение для гидов
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function greetingForGuide($strEMail) {
    $m = new Service_Mail(self::_greetingForGuideSubject(),
                    self::_mailHello() .
                    self::_greetingForGuide() .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _greetingForGuideSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "greeting_for_guide"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _greetingForGuide() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "greeting_for_guide"');
    return $text;
  }


 /**
   * Приветственно сообщение для гидов
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function greetingForTourist($strEMail) {
    $m = new Service_Mail(self::_greetingForTouristSubject(),
                    self::_mailHello() .
                    self::_greetingForTourist() .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _greetingForTouristSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "greeting_for_tourist"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _greetingForTourist() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "greeting_for_tourist"');
    return $text;
  }

	/**
	 * Получаем все уведомления для редактирования
	 *
	 * @return unknown
	 */
  public static function getAllForAdmin() {
  	$objDB = Service_Database::instance();
  	$stmt = 'SELECT * FROM `notification_text` ORDER BY `sort_num` DESC, `function` DESC';
  	return $objDB->select($stmt);

  }

  /**
   * Метод высылает восстановленный пароль
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function afterOrderComplete($strEMail, $arrGuide, $arrUser, $city) {
    $m = new Service_Mail(self::_afterOrderCompleteSubject($arrUser),
                    self::_mailHello() .
                    self::_afterOrderComplete($strEMail, $arrGuide, $arrUser, $city) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _afterOrderCompleteSubject($arrUser) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "after_order_complete"');
  	$text = str_replace('[login]', $arrUser['login'], $text);
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _afterOrderComplete($strEMail, $arrGuide, $arrUser, $city) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "after_order_complete"');

  	$key = md5($strEMail . 'superSlovo');
  	$form = '<style>.reviewText{width:500px;height:160px;margin-bottom:7px;}.reviewTable{text-align:center;}.reviewTable th{text-align:left;}</style>';
  	$form .= '<form action="' . W_FULLPATH . 'ajax/review/addReview/guide/' . $arrGuide['ID_guide'] . '/" method="post">' . View_Order::getReviewForm() . '<br /><input type="submit" value="Submit" /><input type="hidden" name="e_mail" value="' . $strEMail . '" /><input type="hidden" name="key" value="' . $key . '" /></form>';

  	$text = str_replace('[form]', $form, $text);
  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[review_link]', View_User::getUserHref($arrUser, true) . 'my_reviews/', $text);
  	$text = str_replace('[city]', $city, $text);
    return $text;
  }

  /**
   * Метод высылает восстановленный пароль
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function afterOrderCompleteThreeDays($strEMail, $arrGuide, $arrUser) {
    $m = new Service_Mail(self::_afterOrderCompleteThreeDaysSubject($arrUser),
                    self::_mailHello() .
                    self::_afterOrderCompleteThreeDays($strEMail, $arrGuide, $arrUser) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _afterOrderCompleteThreeDaysSubject($arrUser) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "after_order_complete_three_days"');
  	$text = str_replace('[login]', $arrUser['login'], $text);
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _afterOrderCompleteThreeDays($strEMail, $arrGuide, $arrUser) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "after_order_complete_three_days"');

  	$key = md5($strEMail . 'superSlovo');
  	$form = '<style>.reviewText{width:500px;height:160px;margin-bottom:7px;}.reviewTable{text-align:center;}.reviewTable th{text-align:left;}</style>';
  	$form .= '<form action="' . W_FULLPATH . 'ajax/review/addReview/guide/' . $arrGuide['ID_user'] . '/" method="post">' . View_Order::getReviewForm() . '<br /><input type="submit" value="Submit" /><input type="hidden" name="e_mail" value="' . $strEMail . '" /><input type="hidden" name="key" value="' . $key . '" /></form>';

  	$text = str_replace('[form]', $form, $text);
  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[review_link]', View_User::getUserHref($arrUser, true) . 'my_reviews/', $text);
  	return $text;
  }

  /**
   * Метод высылает восстановленный пароль
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function sendOrderConfirmation($strEMail, $arrGuide, $arrUser, $arrOrder) {
    $m = new Service_Mail(self::_sendOrderConfirmationSubject(),
                    self::_mailHello() .
                    self::_sendOrderConfirmation($strEMail, $arrGuide, $arrUser, $arrOrder) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _sendOrderConfirmationSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_order_confirmation"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _sendOrderConfirmation($strEMail, $arrGuide, $arrUser, $arrOrder) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_order_confirmation"');

  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[number]', $arrOrder['ID_order'], $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[tour_date]', Service_Date::BDDateToHuman2($arrOrder['date']), $text);
  	$text = str_replace('[meeting_time]', $arrOrder['time'], $text);
  	$text = str_replace('[meeting_point]', $arrOrder['hotel'], $text);
  	$text = str_replace('[duration]', $arrOrder['hours'], $text);
  	$text = str_replace('[people]', $arrOrder['people_number'], $text);
  	$text = str_replace('[contact_phone]', $arrGuide['phone_number'], $text);
  	$text = str_replace('[contact_e_mail]', $arrGuide['e_mail'], $text);
  	$text = str_replace('[comment]', $arrGuide['comment'], $text);
  	$text = str_replace('[price]', $arrOrder['amount'], $text);
  	$text = str_replace('[deposit]', max($arrOrder['amount'] / 10, 10), $text);
  	$text = str_replace('[cash]', $arrOrder['amount'] - max($arrOrder['amount'] / 10, 10), $text);

  	return $text;
  }

  /**
   * Метод высылает восстановленный пароль
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function sendOrderDecline($strEMail, $arrGuide, $arrUser, $arrOrder) {
    $m = new Service_Mail(self::_sendOrderDeclineSubject(),
                    self::_mailHello() .
                    self::_sendOrderDecline($strEMail, $arrGuide, $arrUser, $arrOrder) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _sendOrderDeclineSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_order_decline"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _sendOrderDecline($strEMail, $arrGuide, $arrUser, $arrOrder) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_order_decline"');

  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[number]', $arrOrder['ID_order'], $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[tour_date]', Service_Date::BDDateToHuman2($arrOrder['date']), $text);
  	$text = str_replace('[meeting_time]', $arrOrder['time'], $text);
  	$text = str_replace('[meeting_point]', $arrOrder['hotel'], $text);
  	$text = str_replace('[duration]', $arrOrder['hours'], $text);
  	$text = str_replace('[people]', $arrOrder['people_number'], $text);
  	$text = str_replace('[contact_phone]', $arrGuide['phone_number'], $text);
  	$text = str_replace('[contact_e_mail]', $arrGuide['e_mail'], $text);
  	$text = str_replace('[price]', $arrOrder['amount'], $text);
  	$text = str_replace('[deposit]', max($arrOrder['amount'] / 10, 10), $text);
  	$text = str_replace('[cash]', $arrOrder['amount'] - max($arrOrder['amount'] / 10, 10), $text);

  	return $text;
  }


  /**
   * Метод высылает восстановленный пароль
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function sendOrderCancel($strEMail, $arrGuide, $arrUser, $arrOrder) {
    $m = new Service_Mail(self::_sendOrderCancelSubject(),
                    self::_mailHello() .
                    self::_sendOrderCancel($strEMail, $arrGuide, $arrUser, $arrOrder) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _sendOrderCancelSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_order_cancel"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _sendOrderCancel($strEMail, $arrGuide, $arrUser, $arrOrder) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_order_cancel"');

  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[number]', $arrOrder['ID_order'], $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[tour_date]', Service_Date::BDDateToHuman2($arrOrder['date']), $text);
  	$text = str_replace('[meeting_time]', $arrOrder['time'], $text);
  	$text = str_replace('[meeting_point]', $arrOrder['hotel'], $text);
  	$text = str_replace('[duration]', $arrOrder['hours'], $text);
  	$text = str_replace('[people]', $arrOrder['people_number'], $text);
  	$text = str_replace('[contact_phone]', $arrGuide['phone_number'], $text);
  	$text = str_replace('[contact_e_mail]', $arrGuide['e_mail'], $text);
  	$text = str_replace('[comment]', $arrGuide['comment'], $text);
  	$text = str_replace('[price]', $arrOrder['amount'], $text);
  	$text = str_replace('[deposit]', max($arrOrder['amount'] / 10, 10), $text);
  	$text = str_replace('[cash]', $arrOrder['amount'] - max($arrOrder['amount'] / 10, 10), $text);

  	return $text;
  }



  /**
   * Метод высылает сообщение гиду о заказе
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function sendOrderForGuide($strEMail, $arrGuide, $arrUser, $arrOrder) {
    $m = new Service_Mail(self::_sendOrderForGuideSubject(),
                    self::_mailHello() .
                    self::_sendOrderForGuide($strEMail, $arrGuide, $arrUser, $arrOrder) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _sendOrderForGuideSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_order_for_guide"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _sendOrderForGuide($strEMail, $arrGuide, $arrUser, $arrOrder) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_order_for_guide"');

  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[number]', $arrOrder['ID_order'], $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[tour_date]', Service_Date::BDDateToHuman2($arrOrder['date']), $text);
  	$text = str_replace('[meeting_time]', $arrOrder['time'], $text);
  	$text = str_replace('[meeting_point]', $arrOrder['hotel'], $text);
  	$text = str_replace('[duration]', $arrOrder['hours'], $text);
  	$text = str_replace('[people]', $arrOrder['people_number'], $text);
  	$text = str_replace('[contact_phone]', $arrGuide['phone_number'], $text);
  	$text = str_replace('[contact_e_mail]', $arrGuide['e_mail'], $text);
  	$text = str_replace('[comment]', $arrOrder['comment'], $text);
  	$text = str_replace('[price]', $arrOrder['amount'], $text);
  	$text = str_replace('[deposit]', max($arrOrder['amount'] / 10, 10), $text);
  	$text = str_replace('[cash]', $arrOrder['amount'] - max($arrOrder['amount'] / 10, 10), $text);
  	return $text;
  }

  /**
   * Метод высылает сообщение гиду о заказе
   *
   * @param string $strEMail
   * @param $pass - пароль
   */
  public static function sendTourOrderForGuide($strEMail, $arrGuide, $arrUser, $arrOrder) {
    $m = new Service_Mail(self::_sendTourOrderForGuideSubject(),
                    self::_mailHello() .
                    self::_sendTourOrderForGuide($strEMail, $arrGuide, $arrUser, $arrOrder) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _sendTourOrderForGuideSubject() {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `subject` FROM `notification_text` WHERE `function` = "send_tour_order_for_guide"');
    return "[" . MAIL_SITE_NAME . "] " . $text;
  }

  private static function _sendTourOrderForGuide($strEMail, $arrGuide, $arrUser, $arrOrder) {
  	$objDB = Service_Database::instance();
  	$text = $objDB->selectSimpleField('SELECT `text` FROM `notification_text` WHERE `function` = "send_tour_order_for_guide"');

  	$text = str_replace('[name]', View_User::getUserName($arrUser), $text);
  	$text = str_replace('[number]', $arrOrder['ID_order'], $text);
  	$text = str_replace('[guide_name]', View_User::getUserName($arrGuide), $text);
  	$text = str_replace('[tour_date]', Service_Date::BDDateToHuman2($arrOrder['date']), $text);
  	$text = str_replace('[meeting_time]', $arrOrder['time'], $text);
  	$text = str_replace('[meeting_point]', $arrOrder['hotel'], $text);
  	$text = str_replace('[duration]', $arrOrder['hours'], $text);
  	$text = str_replace('[people]', $arrOrder['people_number'], $text);
  	$text = str_replace('[contact_phone]', $arrGuide['phone_number'], $text);
  	$text = str_replace('[contact_e_mail]', $arrGuide['e_mail'], $text);
  	$text = str_replace('[comment]', $arrOrder['comment'], $text);
  	$text = str_replace('[price]', $arrOrder['amount'], $text);
  	$text = str_replace('[deposit]', max($arrOrder['amount'] / 10, 10), $text);
  	$text = str_replace('[cash]', $arrOrder['amount'] - max($arrOrder['amount'] / 10, 10), $text);
  	return $text;
  }


  /**
   * Метод уведомляет администратора о появлении нового сообщения в разделе help
   *
   * @param string $name
   * @param string $userMail
   * @param string $text
   */
  public static function newHelpNotification($name, $userMail, $text) {
  	//$strEMail = 'kouzma.d@gmail.com';
  	$strEMail = 'tora.borstrom@privateguide.com';
    $m = new Service_Mail(self::_newHelpNotificationSubject(),
                    self::_mailHello() .
                    self::_newHelpNotification($name, $userMail, $text) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _newHelpNotificationSubject() {
    return "[" . MAIL_SITE_NAME . "] new message in the 'help'";
  }


  private static function _newHelpNotification($name, $userMail, $text) {
    return "<html>There is a new message in the 'help'<br /><br /><b>Name:</b> " . $name . "<br /><b>E-mail: </b>" . $userMail . "<br /><b>Text: </b>" . $text;
  }

  /**
   * Метод уведомляет администратора о появлении нового сообщения в разделе what people are saying
   *
   * @param string $name
   * @param string $userMail
   * @param string $text
   */
  public static function newSimpleReviewNotification($name, $userMail, $text) {
  	//$strEMail = 'kouzma.d@gmail.com';
  	$strEMail = 'tora.borstrom@privateguide.com';
    $m = new Service_Mail(self::_newSimpleReviewNotificationSubject(),
                    self::_mailHello() .
                    self::_newSimpleReviewNotification($name, $userMail, $text) .
                    self::_mailEnd(),
                    array($strEMail)
                   );
    $m->send();
  }

  private static function _newSimpleReviewNotificationSubject() {
    return "[" . MAIL_SITE_NAME . "] new message in the 'what people are saying'";
  }


  private static function _newSimpleReviewNotification($name, $userMail, $text) {
    return "<html>There is a new message in the 'what people are saying'<br /><br /><b>Name:</b> " . $name . "<br /><b>E-mail: </b>" . $userMail . "<br /><b>Text: </b>" . $text;
  }

}
?>